﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CourseSystem.Common.Helpers;
using CourseSystem.Core.BLL;
using CourseSystem.Core.Entities;

namespace CourseSystem.Stat
{
    public class LearnProcessStat
    {
        public static Dictionary<int, LearnProcessModel> LearnSectionList;

        static LearnProcessStat()
        {
            //初始化
            LearnSectionList = new Dictionary<int, LearnProcessModel>();
        }

        public static void Init(int uid)
        {
            if (!LearnSectionList.ContainsKey(uid))
            {
                LearnSectionList.Add(uid, new LearnProcessModel { StartTime = DateTime.Now, SectionList = new List<int> { } });
            }
        }

        public static void LearnSection(int id, int userId, DateTime startTime)
        {
            if (id == 0)
                return;
            LearnProcessModel info;
            if (LearnSectionList.TryGetValue(userId, out info))
            {
                if (!info.SectionList.Contains(id))
                    info.SectionList.Add(id);
                return;
            }
            LearnSectionList.Add(userId, new LearnProcessModel { StartTime = startTime, SectionList = new List<int> { id } });
        }

        //用户登出事件
        public static void UpdateUser(int id)
        {
            LearnProcessModel info;
            if (!LearnSectionList.TryGetValue(id, out info))
                return;
            if (info == null)
            {
                LearnSectionList.Remove(id);
                return;
            }
            var now = DateTime.Now;
            var learnSection = "";
            var lastLearnSection = "";
            foreach (var item in info.SectionList)
            {
                var sectionInfo = SectionBLL.Get(item);
                if (sectionInfo == null)
                    continue;
                learnSection = learnSection + (sectionInfo.ChapterInfo.Name + " ");
                learnSection = learnSection + (sectionInfo.Name + "|");
            }
            if (string.IsNullOrEmpty(learnSection))
            {

                learnSection = "未学习任何章节.";
                lastLearnSection = learnSection;
            }
            if (info.SectionList != null && info.SectionList.Count > 0)
            {
                var sectionInfo = SectionBLL.Get(info.SectionList.Last());
                if (sectionInfo == null)
                {
                    lastLearnSection = "未学习任何章节."; ;
                }
                else
                {
                    lastLearnSection = sectionInfo.ChapterInfo.Name + " "  + sectionInfo.Name;
                }
            }
            var add = new LearnProcessInfo
                          {
                              LearnSection = learnSection,
                              LastSection = lastLearnSection,
                              QuestionCount = QuestionBLL.GetCount(id, info.StartTime, now),
                              StudentId = id,
                              TopicCount = TopicsBLL.GetCount(id, info.StartTime, now),
                              StartTime = info.StartTime,
                              EndTime = now,
                              LearnTime = (now - info.StartTime).Minutes,
                              SectionCount = info.SectionList.Count
                          };
            LearnProcessBLL.Add(add);
            LearnSectionList.Remove(id);
        }
    }
}
